import api from '@/api'

const state = {
    orderList: [],
    orderInfo: {}
}

const actions = {
    // 获取订单列表
    async getOrderList({ commit }, params) {
        let result = await api.reqOrderList(params)
        if (result.code === 200) {
            commit('SET_ORDER_LIST', result.data)
            return 'ok'
        } else {
            return Promise.reject(new Error('获取订单失败'))
        }
    },
    // 创建订单
    async createOrder({ commit }, data) {
        let result = await api.reqCreateOrder(data)
        if (result.code === 200) {
            return result.data
        } else {
            return Promise.reject(new Error('创建订单失败'))
        }
    }
}

const mutations = {
    SET_ORDER_LIST(state, orderList) {
        state.orderList = orderList
    },
    SET_ORDER_INFO(state, orderInfo) {
        state.orderInfo = orderInfo
    }
}

const getters = {
    orderList: state => state.orderList || [],
    orderInfo: state => state.orderInfo || {}
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}